<?php

// 4/26/2010: immanuel192 write this action to get bible ajax search

if (!defined('EXPONENT')) exit('');

global $db;

function decodeURI($string) {
   $result = "";
   for ($i = 0; $i < strlen($string); $i++) {
       $decstr = "";
       for ($p = 0; $p <= 8; $p++) {
          $decstr .= $string[$i+$p];
       } 
       list($decodedstr, $num) = decodeURIbycharacter($decstr);
       $result .= urldecode($decodedstr);
       $i += $num ;
   }
   return $result;
}

function decodeURIbycharacter($str) {

   $char = $str;

   if ($char == "%E2%82%AC") { return array("%80", 8); }
   if ($char == "%E2%80%9A") { return array("%82", 8); }
   if ($char == "%E2%80%9E") { return array("%84", 8); }
   if ($char == "%E2%80%A6") { return array("%85", 8); }
   if ($char == "%E2%80%A0") { return array("%86", 8); }
   if ($char == "%E2%80%A1") { return array("%87", 8); }
   if ($char == "%E2%80%B0") { return array("%89", 8); }
   if ($char == "%E2%80%B9") { return array("%8B", 8); }
   if ($char == "%E2%80%98") { return array("%91", 8); }
   if ($char == "%E2%80%99") { return array("%92", 8); }
   if ($char == "%E2%80%9C") { return array("%93", 8); }
   if ($char == "%E2%80%9D") { return array("%94", 8); }
   if ($char == "%E2%80%A2") { return array("%95", 8); }
   if ($char == "%E2%80%93") { return array("%96", 8); }
   if ($char == "%E2%80%94") { return array("%97", 8); }
   if ($char == "%E2%84%A2") { return array("%99", 8); }
   if ($char == "%E2%80%BA") { return array("%9B", 8); }

   $char = substr($str, 0, 6);

   if ($char == "%C2%81") { return array("%81", 5); }
   if ($char == "%C6%92") { return array("%83", 5); }
   if ($char == "%CB%86") { return array("%88", 5); }
   if ($char == "%C5%A0") { return array("%8A", 5); }
   if ($char == "%C5%92") { return array("%8C", 5); }
   if ($char == "%C2%8D") { return array("%8D", 5); }
   if ($char == "%C5%BD") { return array("%8E", 5); }
   if ($char == "%C2%8F") { return array("%8F", 5); }
   if ($char == "%C2%90") { return array("%90", 5); }
   if ($char == "%CB%9C") { return array("%98", 5); }
   if ($char == "%C5%A1") { return array("%9A", 5); }
   if ($char == "%C5%93") { return array("%9C", 5); }
   if ($char == "%C2%9D") { return array("%9D", 5); }
   if ($char == "%C5%BE") { return array("%9E", 5); }
   if ($char == "%C5%B8") { return array("%9F", 5); }
   if ($char == "%C2%A0") { return array("%A0", 5); }
   if ($char == "%C2%A1") { return array("%A1", 5); }
   if ($char == "%C2%A2") { return array("%A2", 5); }
   if ($char == "%C2%A3") { return array("%A3", 5); }
   if ($char == "%C2%A4") { return array("%A4", 5); }
   if ($char == "%C2%A5") { return array("%A5", 5); }
   if ($char == "%C2%A6") { return array("%A6", 5); }
   if ($char == "%C2%A7") { return array("%A7", 5); }
   if ($char == "%C2%A8") { return array("%A8", 5); }
   if ($char == "%C2%A9") { return array("%A9", 5); }
   if ($char == "%C2%AA") { return array("%AA", 5); }
   if ($char == "%C2%AB") { return array("%AB", 5); }
   if ($char == "%C2%AC") { return array("%AC", 5); }
   if ($char == "%C2%AD") { return array("%AD", 5); }
   if ($char == "%C2%AE") { return array("%AE", 5); }
   if ($char == "%C2%AF") { return array("%AF", 5); }
   if ($char == "%C2%B0") { return array("%B0", 5); }
   if ($char == "%C2%B1") { return array("%B1", 5); }
   if ($char == "%C2%B2") { return array("%B2", 5); }
   if ($char == "%C2%B3") { return array("%B3", 5); }
   if ($char == "%C2%B4") { return array("%B4", 5); }
   if ($char == "%C2%B5") { return array("%B5", 5); }
   if ($char == "%C2%B6") { return array("%B6", 5); }
   if ($char == "%C2%B7") { return array("%B7", 5); }
   if ($char == "%C2%B8") { return array("%B8", 5); }
   if ($char == "%C2%B9") { return array("%B9", 5); }
   if ($char == "%C2%BA") { return array("%BA", 5); }
   if ($char == "%C2%BB") { return array("%BB", 5); }
   if ($char == "%C2%BC") { return array("%BC", 5); }
   if ($char == "%C2%BD") { return array("%BD", 5); }
   if ($char == "%C2%BE") { return array("%BE", 5); }
   if ($char == "%C2%BF") { return array("%BF", 5); }
   if ($char == "%C3%80") { return array("%C0", 5); }
   if ($char == "%C3%81") { return array("%C1", 5); }
   if ($char == "%C3%82") { return array("%C2", 5); }
   if ($char == "%C3%83") { return array("%C3", 5); }
   if ($char == "%C3%84") { return array("%C4", 5); }
   if ($char == "%C3%85") { return array("%C5", 5); }
   if ($char == "%C3%86") { return array("%C6", 5); }
   if ($char == "%C3%87") { return array("%C7", 5); }
   if ($char == "%C3%88") { return array("%C8", 5); }
   if ($char == "%C3%89") { return array("%C9", 5); }
   if ($char == "%C3%8A") { return array("%CA", 5); }
   if ($char == "%C3%8B") { return array("%CB", 5); }
   if ($char == "%C3%8C") { return array("%CC", 5); }
   if ($char == "%C3%8D") { return array("%CD", 5); }
   if ($char == "%C3%8E") { return array("%CE", 5); }
   if ($char == "%C3%8F") { return array("%CF", 5); }
   if ($char == "%C3%90") { return array("%D0", 5); }
   if ($char == "%C3%91") { return array("%D1", 5); }
   if ($char == "%C3%92") { return array("%D2", 5); }
   if ($char == "%C3%93") { return array("%D3", 5); }
   if ($char == "%C3%94") { return array("%D4", 5); }
   if ($char == "%C3%95") { return array("%D5", 5); }
   if ($char == "%C3%96") { return array("%D6", 5); }
   if ($char == "%C3%97") { return array("%D7", 5); }
   if ($char == "%C3%98") { return array("%D8", 5); }
   if ($char == "%C3%99") { return array("%D9", 5); }
   if ($char == "%C3%9A") { return array("%DA", 5); }
   if ($char == "%C3%9B") { return array("%DB", 5); }
   if ($char == "%C3%9C") { return array("%DC", 5); }
   if ($char == "%C3%9D") { return array("%DD", 5); }
   if ($char == "%C3%9E") { return array("%DE", 5); }
   if ($char == "%C3%9F") { return array("%DF", 5); }
   if ($char == "%C3%A0") { return array("%E0", 5); }
   if ($char == "%C3%A1") { return array("%E1", 5); }
   if ($char == "%C3%A2") { return array("%E2", 5); }
   if ($char == "%C3%A3") { return array("%E3", 5); }
   if ($char == "%C3%A4") { return array("%E4", 5); }
   if ($char == "%C3%A5") { return array("%E5", 5); }
   if ($char == "%C3%A6") { return array("%E6", 5); }
   if ($char == "%C3%A7") { return array("%E7", 5); }
   if ($char == "%C3%A8") { return array("%E8", 5); }
   if ($char == "%C3%A9") { return array("%E9", 5); }
   if ($char == "%C3%AA") { return array("%EA", 5); }
   if ($char == "%C3%AB") { return array("%EB", 5); }
   if ($char == "%C3%AC") { return array("%EC", 5); }
   if ($char == "%C3%AD") { return array("%ED", 5); }
   if ($char == "%C3%AE") { return array("%EE", 5); }
   if ($char == "%C3%AF") { return array("%EF", 5); }
   if ($char == "%C3%B0") { return array("%F0", 5); }
   if ($char == "%C3%B1") { return array("%F1", 5); }
   if ($char == "%C3%B2") { return array("%F2", 5); }
   if ($char == "%C3%B3") { return array("%F3", 5); }
   if ($char == "%C3%B4") { return array("%F4", 5); }
   if ($char == "%C3%B5") { return array("%F5", 5); }
   if ($char == "%C3%B6") { return array("%F6", 5); }
   if ($char == "%C3%B7") { return array("%F7", 5); }
   if ($char == "%C3%B8") { return array("%F8", 5); }
   if ($char == "%C3%B9") { return array("%F9", 5); }
   if ($char == "%C3%BA") { return array("%FA", 5); }
   if ($char == "%C3%BB") { return array("%FB", 5); }
   if ($char == "%C3%BC") { return array("%FC", 5); }
   if ($char == "%C3%BD") { return array("%FD", 5); }
   if ($char == "%C3%BE") { return array("%FE", 5); }
   if ($char == "%C3%BF") { return array("%FF", 5); }
   
   $char = substr($str, 0, 3);
   if ($char == "%20") { return array("+", 2); }
   
   $char = substr($str, 0, 1);

   if ($char == "!") { return array("%21", 0); }
   if ($char == "#") { return array("%23", 0); }
   if ($char == "$") { return array("%24", 0); }
   if ($char == "&") { return array("%26", 0); }
   if ($char == "\"") { return array("%27", 0); }
   if ($char == "(") { return array("%28", 0); }
   if ($char == ")") { return array("%29", 0); }
   if ($char == "*") { return array("%2A", 0); }
   if ($char == "+") { return array("%2B", 0); }
   if ($char == ",") { return array("%2C", 0); }
   if ($char == "/") { return array("%2F", 0); }
   if ($char == ":") { return array("%3A", 0); }
   if ($char == ";") { return array("%3B", 0); }
   if ($char == "=") { return array("%3D", 0); }
   if ($char == "?") { return array("%3F", 0); }
   if ($char == "@") { return array("%40", 0); }
   if ($char == "~") { return array("%7E", 0); }

   if ($char == "%") {
      return array(substr($str, 0, 3), 2);
   } else {
      return array($char, 0);
   }
}

$i18n = exponent_lang_loadFile('modules/biblemodule/bible.php');
// get t2 value, just for fun
$t2=$_REQUEST['t2'];
if (isset($_REQUEST['book_sf']) && isset($_REQUEST['book_st']) && isset($_REQUEST['translation'])
    && isset($_REQUEST['chapter_sf']) && isset($_REQUEST['chapter_st'])
    && isset($_REQUEST['verse_sf']) && isset($_REQUEST['verse_st'])
    && isset($_REQUEST['search']))
{
	$translation = intval($_REQUEST['translation']);
	$book_sf = intval($_REQUEST['book_sf']);
	$book_st = intval($_REQUEST['book_st']);
	$chapter_sf = intval($_REQUEST['chapter_sf']);
	$chapter_st = intval($_REQUEST['chapter_st']);
	$verse_sf = intval($_REQUEST['verse_sf']);
	$verse_st = intval($_REQUEST['verse_st']);
	
	$search = strip_tags(urldecode($_REQUEST['search']));
	$search_backup=$search;
	// remove ' and "
	$search=strtolower($search);
	$search = str_replace("'","",$search); $search = str_replace("\"","",$search);
	// some stuff fix
	// replace giê-xu with jêsus 
	$search = str_replace("giê-xu","jêsus",$search);
	$search = str_replace("giêxu","jêsus",$search);
	// end of stuff
	//echo $search;
	// get translation sortname
	$bible=$db->selectObject("bibles","id = " . $translation);
	if ($bible == null) 
	{
		die();
	}
	else
	{
	
		$short_name = $bible->short_name;
		// build sql WHERE clause
		$search=str_replace("  "," ",$search);
		$search=str_replace("  &"," &",$search);
		$search=str_replace("&  ","& ",$search);
		
		$temp_search=array();
		while (0<1)
		{
			$search_pos=strpos($search,"[");
			//echo "pos 1 = " .$search_pos;
			if ($search_pos===false) break;
			$search_pos2=strpos($search,"]");
			//echo "pos 2 = ".$search_pos2;
			if ($search_pos2===false) break;
			
			$s=substr($search,$search_pos+1,$search_pos2-$search_pos-1);
			$temp_search[]=$s;
			$search=substr($search,0,$search_pos).substr($search,$search_pos2+1);
		}
		
		$search=explode(" ",$search);
		foreach ($search as $k=>$v)
			$temp_search[]=$v;
		$search=$temp_search;
		
		$sql="";
		if ($book_sf == $book_st)
			$sql="(book = {$book_sf})";
		else
			$sql="(book >= {$book_sf}) AND (book<={$book_st})";
			
		if (($book_sf == $book_st) && ($chapter_sf == $chapter_st))
		{
			if ($sql !="") $sql.=" AND ";
			$sql.="(chapter = {$chapter_sf})";
		}
			
		// for full text search
		/*$sql_full_text=" MATCH(LOWER(bibletext)) AGAINST('";
		foreach ($search as $k=>$v)
		{
			if (trim($v)=="") continue;
			$sql_full_text.="\"{$v}\" ";
		}
		$sql_full_text.="' IN BOOLEAN MODE) ";*/
		
		// and for book and chapter
		$sql_full_text="";
		foreach ($search as $k=>$v)
		{
			if (trim($v)=="") continue;
			$sql_full_text.=" AND (bibletext LIKE '% {$v} %' COLLATE utf8_unicode_ci)";
			//$sql_full_text.=" AND (bibletext REGEXP BINARY '^{$v}' COLLATE utf8_unicode_ci)";
		}
		$sql.=" AND (bible_id = {$translation}) {$sql_full_text}  ORDER BY  book, chapter, verse";
		
		$sql="SELECT {$db->prefix}bible_{$short_name}.*, {$db->prefix}bible_info.book_name book_name
			FROM {$db->prefix}bible_{$short_name} INNER JOIN {$db->prefix}bible_info
			ON {$db->prefix}bible_{$short_name}.book = {$db->prefix}bible_info.bindex
			WHERE {$sql} ";
		//echo $sql;
		// okies, here we get data
		$data=$db->selectObjectsBySql($sql);
		//echo "Found ".count($data);
		// okies, here we remove all result that not in condition range
		// for the begining
		foreach ($data as $k=>$v)
		{
			if ($v->book != $book_sf) break;
			if (($v->chapter < $chapter_sf) || ($v->verse < $verse_sf))
			{
				//echo "Unset1 {$v->chapter}:{$v->verse}\r\n";
				unset($data[$k]); continue;
			}
		}
		$data=array_reverse($data);
		// for the end
		foreach ($data as $k=>$v)
		{
			if ($v->book != $book_st)break;
			if (($v->chapter > $chapter_st) || ($v->verse > $verse_st))
			{
				//echo "Unset2 {$v->chapter}:{$v->verse}\r\n";
				unset($data[$k]); continue;
			}
		}
		// 
		$data=array_reverse($data);
		
		//echo "After trim, remains " . count($data);
		echo $t2; 
		if (count($data) == 0)
		{
			// hehe, search empty
			$ret_string=$i18n['search_empty'];
			$ret_string=str_replace("%",$search_backup,$ret_string);
			echo "<div>{$ret_string}</div>";
			die();
		}
		$ret_string=$i18n['search_result'];
		$ret_string=str_replace("%1",count($data),$ret_string);
		$ret_string=str_replace("%2",$search_backup,$ret_string);
		echo "<div style='color: #545454;'>{$ret_string}</div><span class='article_separator'>&nbsp;</span>";
		// okies, here we show the bible
		$last_book=-1; $last_chapter=-1;
		foreach ($data as $k=>$v)
		{
			// show the book and chapter
			if (($last_book != $v->book) || ($last_chapter != $v->chapter))
			{
				// update book, chapter and show the title
				$last_book=$v->book;
				$last_chapter=$v->chapter;
				echo "<div style='padding-top: 15px;'><b>{$v->book_name}</b>&nbsp;<span>{$last_chapter}</span></div>";
			}
			echo "<div>";
			echo "<span id='v{$v->verse}'>{$v->verse}</span>&nbsp;";
			echo ($v->bibletext . "&nbsp;");
			echo "</div>";
		}
	}
}
else
{
	die();
}
?>
